package com.dw.love.dwade.view.fragment;

import android.content.Intent;
import android.content.res.TypedArray;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.blankj.utilcode.util.ToastUtils;
import com.dw.love.dwade.R;
import com.dw.love.dwade.data.ImageWade;
import com.dw.love.dwade.view.PhotoViewActivity;
import com.dw.love.dwade.view.adapter.HomePicAdapter;
import com.dw.love.dwade.view.adapter.viewholder.HomePicViewHolder;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.jzvd.JZVideoPlayer;

import static android.support.constraint.Constraints.TAG;

/**
 * Created by luozizhao on 2018/6/14.
 */

public class FragmentHomePic extends Fragment {

    @BindView(R.id.rv_home_pic)
    RecyclerView rvHomePic;
    Unbinder unbinder;
    int[] imgRes;
    private HomePicAdapter mHomePicAdapter;
    private List<ImageWade> mImageWadeList = new ArrayList<>();
    private final static String TAG = "FragmentHomePic";

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_home_pic, container, false);
        unbinder = ButterKnife.bind(this, view);
        initData();
        initView();
        return view;
    }

    private void initView() {
        initImgArray();
        StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
        rvHomePic.addItemDecoration(new FlowDecoration());
        rvHomePic.setLayoutManager(staggeredGridLayoutManager);
//        rvHomePic.setNestedScrollingEnabled(false);
        mHomePicAdapter = new HomePicAdapter(mImageWadeList, getActivity());
        rvHomePic.setAdapter(mHomePicAdapter);

    /*    rvHomePic.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                // 查看源码可知State有三种状态：SCROLL_STATE_IDLE（静止）、SCROLL_STATE_DRAGGING（上升）、SCROLL_STATE_SETTLING（下落）
                if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                    //滚动静止时才加载图片资源，极大提升流畅度
                    mHomePicAdapter.setScrolling(false);
                    mHomePicAdapter.notifyDataSetChanged();
                } else {
                    mHomePicAdapter.setScrolling(true);
                }
                super.onScrollStateChanged(recyclerView, newState);
            }

        });*/


        mHomePicAdapter.setOnClickImageListener(new HomePicAdapter.OnClickImageView() {
            @Override
            public void clickItem(String path, int position) {
                Intent intent = new Intent(getActivity(), PhotoViewActivity.class);
                intent.putExtra("path", path);
                startActivity(intent);
            }
        });
    }

    private void initData() {

        BmobQuery<ImageWade> query = new BmobQuery<ImageWade>();
        query.addQueryKeys("objectId,name,icon,descripe");
        query.findObjects(new FindListener<ImageWade>() {
            @Override
            public void done(List<ImageWade> list, BmobException e) {
                if (e == null) {
                    mImageWadeList.addAll(list);
                    mHomePicAdapter.notifyDataSetChanged();
                } else {
                    Log.d(TAG, "done: 加载失败"+e.getMessage());
                }
            }
        });
    }

    private void initImgArray() {
        TypedArray typedArray = getResources().obtainTypedArray(R.array.icon_array_wade);
        int len = typedArray.length();
        imgRes = new int[len];
        for (int i = 0; i < len; i++) {
            imgRes[i] = typedArray.getResourceId(i, 0);
        }
        typedArray.recycle();
    }


    class FlowDecoration extends RecyclerView.ItemDecoration {
        @Override
        public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
            outRect.left = 8;
            outRect.right = 8;
            outRect.bottom = 8;
        }
    }


    @Override
    public void onPause() {
        super.onPause();
        JZVideoPlayer.releaseAllVideos();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
